﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Sharp.Portal.Mvc.Bootstrap
{
    public static class LableBuilderExtensions
    {
        public static LableBuilder Lable(this BootstrapFactory factory)
        {
            return new LableBuilder();
        }
        public static LableBuilder LableFor<TModel, TProperty>(this BootstrapFactory<TModel> factory,
            Expression<Func<TModel, TProperty>> expression)
        {
            return factory.UnobtrusiveValidationAttributes(factory.Lable(), expression);
        }
        private static LableBuilder UnobtrusiveValidationAttributes<TModel, TProperty>(this BootstrapFactory<TModel> factory, LableBuilder builder, Expression<Func<TModel, TProperty>> expression)
        {
            var htmlHelper = factory.HtmlHelper;

            var name = ExpressionHelper.GetExpressionText(expression);

            var fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            var modelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            
            builder.Name(fullHtmlFieldName);

            var value = htmlHelper.FormatValue(modelMetadata.Model, null);

            builder.Text(value);

           

            return builder;
        }
    }
}
